from .base import db
from datetime import datetime

class DictionaryType(db.Model):
    __tablename__ = 'dictionary_type'

    id = db.Column(db.Integer, primary_key=True)
    code = db.Column(db.String(64), unique=True, nullable=False, comment='类型编码')
    name = db.Column(db.String(128), nullable=False, comment='类型名称')
    description = db.Column(db.String(255), comment='描述')
    status = db.Column(db.SmallInteger, default=1, comment='状态 1启用 0禁用')
    create_time = db.Column(db.DateTime, default=datetime.utcnow)
    update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    items = db.relationship('DictionaryItem', backref='type', lazy=True, cascade='all, delete-orphan')

    def to_dict(self):
        return {
            'id': self.id,
            'code': self.code,
            'name': self.name,
            'description': self.description,
            'status': self.status,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
        } 